# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/media-fonts/corefonts/corefonts-1-r4.ebuild,v 1.6 2008/06/16 03:45:57 josejx Exp $

inherit font

DESCRIPTION="czcionki.com TrueType polish fonts"
HOMEPAGE="http://www.digit.pl/czcionki/"

SRC_URI="http://czcionki.download.idg.pl/fack/34786b423eb30c033f00317a91f87580/49537028/polskie.zip
    http://czcionki.download.idg.pl/fack/8888e5041c0cb5ff44416eb811bdab6b/49537054/polskie2.zip
    http://czcionki.download.idg.pl/fack/76e4759962398b5ee90c78ac289ebe93/49537062/polskie3.zip
    http://czcionki.download.idg.pl/fack/bf625167f33b765e68f3e3fb88783d3c/49537082/polskie4.zip
    http://czcionki.download.idg.pl/fack/53d5a3766d2e947bc01740e37d55c37b/4953708e/polskie5.zip
    http://czcionki.download.idg.pl/fack/d0ef26507d8ebf4e7a1eab341476d760/49537097/polskie6.zip
    http://czcionki.download.idg.pl/fack/b8b8378f2808ec01474fbf7c3b45f75d/495370a0/polskie7.zip
    http://czcionki.download.idg.pl/fack/a223b76ac56a60b9014d4d82fde41331/495370aa/polskie8.zip
    http://czcionki.download.idg.pl/fack/d3d94b8892884ed597acb792457cf789/495370b3/polskie9.zip
    http://czcionki.download.idg.pl/fack/28424f84a6ff438743a92d3b4f05faf6/495370bc/polskie10.zip
    http://czcionki.download.idg.pl/fack/d79949a296d785741bce30cc1fbf03ff/495370c3/polskie11.zip
    http://czcionki.download.idg.pl/fack/96e572509dfcdcddc89d7a0b7e922684/495370cd/polskie12.zip
    http://czcionki.download.idg.pl/fack/011d09a7d199013067f32fff554dcc26/495370d4/polskie13.zip
    http://czcionki.download.idg.pl/fack/9c05a301fe223c8ca5826c6c68717e96/495370dd/polskie14.zip
    http://czcionki.download.idg.pl/fack/720be2113c166be86ac28d3882ab4265/495370e5/polskie15.zip
    http://czcionki.download.idg.pl/fack/d7ea26fd0bf73c1413708f250cd7c4b6/495370ee/polskie16.zip
    http://czcionki.download.idg.pl/fack/a344fb1239703e03bb72ab6efea3d252/495370f6/polskie17.zip
    http://czcionki.download.idg.pl/fack/25ff5d9c4c144c34cfd3c8bfe8e848a8/495370fe/polskie18.zip
    http://czcionki.download.idg.pl/fack/9eb0afc8245061d6f01152e2cba611d4/49537107/polskie19.zip"

LICENSE="Freeware"
SLOT="0"
KEYWORDS="amd64 x86"
IUSE="X"

DEPEND="app-arch/p7zip app-arch/unrar"
RDEPEND=""

S=${WORKDIR}
FONT_S=${WORKDIR}
FONT_SUFFIX="ttf"

src_unpack() {
    for zip in ${A} ; do
        TEMP_NAME=${WORKDIR}/temp/${zip%%.*}
        
        echo ">>> Unpacking ${zip} to ${TEMP_NAME}"
        mkdir -p $TEMP_NAME
        unzip -qqL ${DISTDIR}/${zip} -x "*.sit" -d $TEMP_NAME >/dev/null 2>&1 \
            || die "failed to unpack ${zip}"

        # Little hack for filenames with strange charset
        for FN in `ls -q ${TEMP_NAME}`; do
            if [ `echo $FN |tr '[:upper:]' '[:lower:]'` != $FN ]; then
                mv ${TEMP_FONTNAME}/${FN} "${TEMP_FONTNAME}/`echo ${FN} |tr '[:upper:]' '[:lower:]'`"
            fi

            FN=`echo $FN |tr '[:upper:]' '[:lower:]'`
            
            if [ `echo $FN |sed 's/[^a-zA-Z0-9 _\.]/_/g'` != $FN ]; then
                echo ">>> Strange charset detected! Changing file name"
                mv ${TEMP_NAME}/$FN "${TEMP_NAME}/`echo $FN |sed 's/[^a-zA-Z0-9 _\.]/_/g'`"
            fi
        done

        for FN in `find ${TEMP_NAME} -type f -exec ls -q {} \;`; do
            FILE=${FN##*/}
            EXT=${FILE#*.}
            BASE=`echo ${FILE%%.*}| sed 's/[^a-zA-Z0-9 _\.]/_/g'`
            
            TEMP_FONTNAME=${WORKDIR}/temp2/$BASE

            echo ">>> Unpacking ${FILE} to ${TEMP_FONTNAME}"
            mkdir -p $TEMP_FONTNAME

            case "$EXT" in
                "zip") unzip -qq -u -o -L ${FN} -d $TEMP_FONTNAME >/dev/null && rm -rf ${FN} ;;
                "rar") unrar e -cl -u -o+ ${FN} $TEMP_FONTNAME/ >/dev/null && rm -rf ${FN} ;;
                *) die "Archive type $EXT not recognized!" ;;
            esac

            # Changing spaces to underscores (loops in bash treats them as delimiter)
            while [ `ls -q ${TEMP_FONTNAME} |sed -e 's/[^\ ]//g' |tr -d "\n" |wc -c` -gt 0 ]; do
                rename " " _ ${TEMP_FONTNAME}/*
            done

            # Little hack for filenames with strange charset
            for FN in `ls -q ${TEMP_FONTNAME}`; do
                if [ `echo $FN |tr '[:upper:]' '[:lower:]'` != $FN ]; then
                    mv ${TEMP_FONTNAME}/${FN} "${TEMP_FONTNAME}/`echo ${FN} |tr '[:upper:]' '[:lower:]'`"
                fi

                FN=`echo $FN |tr '[:upper:]' '[:lower:]'`
              
                if [ `echo $FN |sed 's/[^a-zA-Z0-9 _\.]/_/g'` != $FN ]; then
                    mv ${TEMP_FONTNAME}/${FN} "${TEMP_FONTNAME}/`echo ${FN} |sed 's/[^a-zA-Z0-9 _\.]/_/g'`"
                fi
            done

        done
    done

    find ${WORKDIR} -name "*.ttf" -exec mv {} ${WORKDIR} \;

    # Docs
    mkdir ${WORKDIR}/docs
    for FN in `find ${WORKDIR}/temp2 -type f`; do
        EFN=${FN%/*}
        mv ${FN} ${WORKDIR}/docs/${EFN##*/}-${FN##*/}
    done
}

src_install() {
    font_src_install
    dodoc docs/*.txt
}
